package org.ehe.erp.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.erp.domain.quality.bo.ErpBomQualityItemBo;
import org.ehe.erp.domain.quality.vo.ErpBomQualityItemVo;
import org.ehe.erp.service.quality.ErpBomQualityItemService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 质检项目与BOM关联
 * @author :zhangnn
 * @className :ErpBomQualityItemController
 * @description: TODO
 * @date 2025-09-10 16:41:12
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/quality/bom/item")
public class ErpBomQualityItemController extends BaseController {
    private final ErpBomQualityItemService bomQualityItemService;


    /**
     * 查询BOM质检项目关联列表（分页）
     */
    @SaCheckPermission("quality:bomItem:list")
    @GetMapping("/list")
    public TableDataInfo<ErpBomQualityItemVo> list(ErpBomQualityItemBo bo, PageQuery pageQuery) {
        return bomQualityItemService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询BOM质检项目关联列表（不分页）
     */
    @SaCheckPermission("quality:bomItem:list")
    @GetMapping("/listAll")
    public R<List<ErpBomQualityItemVo>> listAll(ErpBomQualityItemBo bo) {
        List<ErpBomQualityItemVo> list = bomQualityItemService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 获取BOM质检项目关联详细信息
     */
    @SaCheckPermission("quality:bomItem:query")
    @GetMapping("/{id}")
    public R<ErpBomQualityItemVo> getInfo(@PathVariable Long id) {
        ErpBomQualityItemVo vo = bomQualityItemService.queryById(id);
        return R.ok(vo);
    }

    /**
     * 新增BOM质检项目关联
     */
    @SaCheckPermission("quality:bomItem:add")
    @Log(title = "BOM质检项目关联", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody List<ErpBomQualityItemBo> bos) {
        return toAjax(bomQualityItemService.insertByBo(bos));
    }

    /**
     * 修改BOM质检项目关联
     */
    @SaCheckPermission("quality:bomItem:edit")
    @Log(title = "BOM质检项目关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpBomQualityItemBo bo) {
        return toAjax(bomQualityItemService.updateByBo(bo));
    }

    /**
     * 删除BOM质检项目关联
     */
    @SaCheckPermission("quality:bomItem:remove")
    @Log(title = "BOM质检项目关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(bomQualityItemService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据BOM ID查询关联的质检项目列表
     */
    @SaCheckPermission("quality:bomItem:query")
    @GetMapping("/bom/{bomId}")
    public R<List<ErpBomQualityItemVo>> getQualityItemsByBomId(@PathVariable Long bomId) {
        List<ErpBomQualityItemVo> list = bomQualityItemService.getQualityItemsByBomId(bomId);
        return R.ok(list);
    }

    /**
     * 根据质检项目ID查询关联的BOM列表
     */
    @SaCheckPermission("quality:bomItem:query")
    @GetMapping("/qualityItem/{qualityItemId}")
    public R<List<ErpBomQualityItemVo>> getBomsByQualityItemId(@PathVariable Long qualityItemId) {
        List<ErpBomQualityItemVo> list = bomQualityItemService.getBomsByQualityItemId(qualityItemId);
        return R.ok(list);
    }

    /**
     * 为BOM批量设置质检项目
     */
    @SaCheckPermission("quality:bomItem:edit")
    @Log(title = "BOM质检项目关联", businessType = BusinessType.UPDATE)
    @PostMapping("/setBomQualityItems")
    public R<Void> setBomQualityItems(@RequestParam Long bomId, @RequestBody List<Long> qualityItemIds) {
        return toAjax(bomQualityItemService.setBomQualityItems(bomId, qualityItemIds));
    }

    /**
     * 为BOM批量设置质检项目（带必检标识）
     */
    @SaCheckPermission("quality:bomItem:edit")
    @Log(title = "BOM质检项目关联", businessType = BusinessType.UPDATE)
    @PostMapping("/setBomQualityItemsWithConfig")
    public R<Void> setBomQualityItemsWithConfig(@RequestParam Long bomId, @RequestBody List<ErpBomQualityItemBo> qualityItems) {
        return toAjax(bomQualityItemService.setBomQualityItemsWithConfig(bomId, qualityItems));
    }

    /**
     * 根据BOM ID删除关联关系
     */
    @SaCheckPermission("quality:bomItem:remove")
    @Log(title = "BOM质检项目关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/bom/{bomId}")
    public R<Void> deleteByBomId(@PathVariable Long bomId) {
        return toAjax(bomQualityItemService.deleteByBomId(bomId));
    }

    /**
     * 根据质检项目ID删除关联关系
     */
    @SaCheckPermission("quality:bomItem:remove")
    @Log(title = "BOM质检项目关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/qualityItem/{qualityItemId}")
    public R<Void> deleteByQualityItemId(@PathVariable Long qualityItemId) {
        return toAjax(bomQualityItemService.deleteByQualityItemId(qualityItemId));
    }

    /**
     * 校验BOM和质检项目关联是否存在
     */
    @GetMapping("/checkRelationExists")
    public R<Boolean> checkRelationExists(@RequestParam Long bomId, @RequestParam Long qualityItemId) {
        return R.ok(bomQualityItemService.checkRelationExists(bomId, qualityItemId));
    }

    /**
     * 复制BOM的质检项目关联到另一个BOM
     */
    @SaCheckPermission("quality:bomItem:edit")
    @Log(title = "BOM质检项目关联", businessType = BusinessType.UPDATE)
    @PostMapping("/copyBomQualityItems")
    public R<Void> copyBomQualityItems(@RequestParam Long sourceBomId, @RequestParam Long targetBomId) {
        return toAjax(bomQualityItemService.copyBomQualityItems(sourceBomId, targetBomId));
    }
}
